
package SSF.OS;

/** IP packet Header class. */
public class IpHeader extends ProtocolMessage {


  /********************* STANDARD CLASS VARIABLES *************************/
  
  /** The version of this IP protocol. */
  public static final String VERSION = "IPv4";

  /** The source IP address of this IP packet. */
  public int SOURCE_IP;
  
  /** The destination IP address of this IP packet. */
  public int DEST_IP;

  /** The protocol number for this header. */
  public int PROTOCOL_NO;

  /** The type of service field.  */
  public byte TOS = 0;
  public static final byte THROUGHPUT_BIT_MASK = 0x8;
  public static final byte DELAY_BIT_MASK = 0x6;

  /** The time-to-live field decremented by each router. */
  public int TIME_TO_LIVE;

  /** Default limit on number of hops : 128 */
  public static final int DEFAULT_TIME_TO_LIVE = 128;

  /******************* Non-STANDARD CLASS VARIABLES ***********************/

  /** The next hop IP address, not in the standard IP header. */
  public int NEXT_HOP_IP;

    
  /*************************** CONSTRUCTORS  ******************************/

  /** IP header constructor. <p>
   @param prot_no Protocol number
   @param src_ip  IP address of packet source
   @param dest_ip IP address of packet destination
   @param ttl     time to live, in hops.
   */
  public IpHeader (int prot_no, int src_ip, int dest_ip, int ttl) {
    PROTOCOL_NO = prot_no;
    SOURCE_IP = src_ip;
    DEST_IP = dest_ip;
    TIME_TO_LIVE = ttl;
  }

  /** IP header constructor with default TTL. <p>
   @param prot_no Protocol number
   @param src_ip  IP address of packet source
   @param dest_ip IP address of packet destination
   */
  public IpHeader (int prot_no, int src_ip, int dest_ip) {
    this(prot_no,src_ip,dest_ip,DEFAULT_TIME_TO_LIVE);
  }

  @Override
public String toString() {
    return "("+VERSION+"/"+
      Protocols.getProtocolByNumber(PROTOCOL_NO)+
      ":s="+IP_s.IPtoString(SOURCE_IP)+
      ":d="+IP_s.IPtoString(DEST_IP)+
      ":n="+IP_s.IPtoString(NEXT_HOP_IP)+
      ":tos="+TOS+
      ":ttl="+TIME_TO_LIVE+")";
  }

  /** Returns the size (in bytes) of the IP header only,
   *  doesn't include the payload, if any.
   */
  @Override
public int header_bytecount(){
    return 20;
  };

  /** Returns the total number of bytes in this IP packet. */
  @Override
public int bytecount() {
    ProtocolMessage tail = payload();
    return (20 + (tail==null?0:tail.bytecount()));
  }

  private Object measurementBucket = null;

  /** Attaches user-defined per-packet measurement data to this IP message.
   *  Introduced by Christopher R. Kelley, NIST, Aug. 2000.
   */

  public void setMeasurementBucket(Object meas) {
    measurementBucket = meas;
  }

  public Object getMeasurementBucket() {
    return measurementBucket;
  }

}


/*=                                                                      =*/
/*=  Copyright (c) 1997--2000  SSF Research Network                      =*/
/*=                                                                      =*/
/*=  SSFNet is open source software, distributed under the GNU General   =*/
/*=  Public License.  See the file COPYING in the 'doc' subdirectory of  =*/
/*=  the SSFNet distribution, or http://www.fsf.org/copyleft/gpl.html    =*/
/*=                                                                      =*/
